﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Logic;
using AutoLoL.Logic.Automation;
using AutoLoL.Logic.Factories;
using AutoLoL.Logic.Masteries;
using AutoLoL.Logic.Masteries.Importers;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Dialogs
{
    /// <summary>
    /// Interaction logic for ImportDefaultItemDialog.xaml
    /// </summary>
    public partial class ImportDefaultItemDialog : Window
    {
        private Champion _champion;

        public Item [] ImportResult { get; private set; }

        public ImportDefaultItemDialog(Window owner, Champion champion, GameType gameType)
        {
            InitializeComponent();
            Owner = owner;

            _champion = champion;

            GameTypeComboBox.ItemsSource = Enum.GetValues(typeof(GameType));
            GameTypeComboBox.SelectedItem = gameType;

            ImportButton.IsDefault = true;
            CancelButton.IsCancel = true;
        }

        private void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            GameTypeComboBox.IsEnabled = false;
            ImportButton.IsEnabled = false;

            LoadingStackPanel.Visibility = Visibility.Visible;

            Grid[] grids = new Grid[] { Item1Grid, Item2Grid, Item3Grid, Item4Grid, Item5Grid, Item6Grid };
            ImportResult = new Item[grids.Length];
            for (int i = 0; i < grids.Length; i++)
            {
                ImportResult[i] = grids[i].DataContext as Item;
            }

            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ImportResult = null;
            Close();
        }

        private void GameTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GameType gameType = (GameType)GameTypeComboBox.SelectedItem;
            if (AutoDeploy.readFromFile(_champion.DirectoryName, gameType))
            {
                ReadOnlyCollection<AutoDeploy.ItemSet> itemSet = AutoDeploy.getItemSets();
                SetNameCombobox.Items.Clear();
                for (int i = 0; i < itemSet.Count; i++)
                {
                    SetNameCombobox.Items.Add(itemSet[i].name);
                }
                //Add this for the riot game defaults
                //SetNameCombobox.Items.Add("Riot Defaults");

                SetNameCombobox.SelectedIndex = 0;
            }
            else
            {
                SetNameCombobox.Items.Clear();
            }
        }

        private void SetNameCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Grid[] grids = new Grid[] { Item1Grid, Item2Grid, Item3Grid, Item4Grid, Item5Grid, Item6Grid };
            if (SetNameCombobox.Items.Count > 0)
            {
                //Check if we are just getting the default LoL ones
                if (SetNameCombobox.SelectedItem.ToString() == "Riot Defaults")
                {
                    //Todo: Grab default items from LoL

                    //Populate the gird with the items
                    /*
                    for (int i = 0; i < grids.Count(); i++)
                    {
                        grids[i].DataContext = ItemFactory.Instance;
                    }
                    */
                }
                else
                {
                    //Get our currently selected list of items
                    ReadOnlyCollection<AutoDeploy.ItemSet> itemSet = AutoDeploy.getItemSets();
                    int index = SetNameCombobox.SelectedIndex;
                    if (index >= itemSet.Count)
                        index = 0;

                    List<int> items = itemSet[index].items;

                    //Populate the gird
                    for (int i = 0; i < items.Count; i++)
                    {
                        grids[i].DataContext = ItemFactory.Instance.GetByItemId(items[i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < grids.Length; i++)
                {
                    grids[i].DataContext = null;
                }
            }
        }
    }
}
